package com.mrobuy.jdvc.controller;

import com.google.gson.Gson;
import com.jd.open.api.sdk.domain.supplier.ProductManagementService.response.find.ApplyProductDto;
import com.jd.open.api.sdk.request.supplier.VcItemProductAppliesFindRequest;
import com.jd.open.api.sdk.response.supplier.VcItemProductAppliesFindResponse;
import com.mrobuy.jd.JdClientFactory;
import com.mrobuy.type.ApplyState;
import com.mrobuy.jdvc.Result;
import com.mrobuy.util.Pagination;
import com.mrobuy.jdvc.dao.VCProductApplyDao;
import com.mrobuy.jdvc.entity.VCProductApply;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
public class ProductApplyController {

	@Autowired
	private VCProductApplyDao vcProductApplyDao;

	@RequestMapping("/productapply")
	public String productapply(Model model) {
		return "productapply";
	}


	@RequestMapping("/productapply/query")
	@ResponseBody
	public Object query(HttpServletRequest request, VCProductApply condition) {

		int start = ServletRequestUtils.getIntParameter(request, "start", 1);
		int length = ServletRequestUtils.getIntParameter(request, "length", 10);

		int draw = ServletRequestUtils.getIntParameter(request, "draw", 1);


		Pagination pagination = vcProductApplyDao.load(start, length, condition);

		//int offset = page*pagesize - pagesize;
		pagination.setDraw(draw);

		return pagination;
	}

	@RequestMapping("/productapply/sync")
	@ResponseBody
	public Result applysync() {

		Result result = new Result(true);

		try {
			int page = 1;
			long count = 10;

			VcItemProductAppliesFindRequest request=new VcItemProductAppliesFindRequest();
			request.setLength(10);

			while ( (page-1)*10 < count ) {
				request.setPage(page++);
				VcItemProductAppliesFindResponse response= JdClientFactory.jdClient().execute(request);
				if ( response.getJosResultDto()==null ) {
					throw new RuntimeException(response.getMsg());
				}

				count = response.getJosResultDto().getCount();
				System.out.println("总记录数目: " + response.getJosResultDto().getCount() + "\n");

				for (ApplyProductDto applyProductDto : response.getJosResultDto().getResult()) {

					System.out.println(new Gson().toJson(applyProductDto) + ": " + ApplyState.getStateByCode(applyProductDto.getState()).desc());

					VCProductApply productApply = vcProductApplyDao.load(applyProductDto.getApplyId());


					//VCProductApply productApply = SQLExecutor.executeQueryBean("select * from VC_PRODUCT_APPLY where APPLYID = ?", VCProductApply.class, applyProductDto.getApplyId());

					if ( productApply==null ) {
						productApply = new VCProductApply();
						productApply.setApplyId(applyProductDto.getApplyId());
						productApply.setWareId(applyProductDto.getWareId());
						productApply.setName(applyProductDto.getName());
						productApply.setSubmitTime(applyProductDto.getSubmitTime());
						productApply.setState(applyProductDto.getState());
						productApply.setZt(1);

						vcProductApplyDao.insert(productApply);
					} else {
						productApply.setApplyId(applyProductDto.getApplyId());
						productApply.setWareId(applyProductDto.getWareId());
						productApply.setName(applyProductDto.getName());
						productApply.setSubmitTime(applyProductDto.getSubmitTime());
						productApply.setState(applyProductDto.getState());
						productApply.setZt(1);

						vcProductApplyDao.update(productApply);
					}
				}
			}
		} catch (Exception e) {
			result.setSuccess(false);
			result.setMsg(e.getMessage());
		}

		return result;
	}

}
